﻿using System;
using System.Collections.Generic;


namespace DBHelper
{
    public static class DicCache
    {
        private static object objLock = new object();
        /// <summary>
        /// 用于缓存对象转换实体
        /// </summary>
        private static Dictionary<string, DictionaryEntries> _DictionaryCache = new Dictionary<string, DictionaryEntries>();
        /// <summary>
        /// 确定是否已经存在缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static DictionaryEntries ExistDictionaryCache(string key)
        {
            DictionaryEntries value;
            _DictionaryCache.TryGetValue(key, out value);
            return value;
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="des"></param>
        private static void Add(string key, DictionaryEntries des)
        {
            lock (objLock)
            {
                if ((!_DictionaryCache.ContainsKey(key)) && des != null)
                {
                    _DictionaryCache.Add(key, des);
                }
            }
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DictionaryEntries GetDictionaryCache(string key)
        {
            DictionaryEntries value;
            _DictionaryCache.TryGetValue(key, out value);
            if (value != null)
            {
                return value;
            }
            else
            {
               // UpdateDictionaryCache();
                _DictionaryCache.TryGetValue(key, out value);
                if (value != null)
                    return value;
                else
                    return null;
            }
            throw new Exception("缓存中没存在此数据");
        }
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        // private static void UpdateDictionaryCache()
        // {
        //     _DictionaryCache = new Dictionary<string, DictionaryEntries>();
        //     IList<DictionaryEntries> result = BaseBusiness.Query<DictionaryEntries>();
        //     foreach (DictionaryEntries model in result)
        //     {
        //         if (model != null && (!_DictionaryCache.ContainsKey(model.Id.ToString())))
        //         {
        //             _DictionaryCache.Add(model.Id.ToString(), model);
        //         }
        //     }

        // }
    }
}
